package com.lsu.component.homework;

import com.lsu.component.section2.AbstractCorp;

import java.util.List;

/**
 * 环境类
 *
 * @Author wang suo
 * @Date 2020/12/22 0022 21:46
 * @Version 1.0
 */
public class Client {
    public static void main(String[] args) {
        //首先是组装一个组织结构出来
        Branch root = compositeCorpTree();

        // 打印根节点
        System.out.println(root.getInfo());

        //然后是所有员工信息
        System.out.println(getTreeInfo(root));
    }

    /**
     * 组装节点
     *
     * @return 返回树叶节点
     */
    private static Branch compositeCorpTree() {
        // 总公司
        Branch root = new Branch("XX公司");

        // 所有的部门
        Branch a = new Branch("技术部");
        Branch b = new Branch("市场部");
        Branch c = new Branch("业务部");
        Branch d = new Branch("行政部");
        Branch e = new Branch("办公室");

        // 所有的叶子部门
        Leaf a1 = new Leaf("Java项目组");
        Leaf a2 = new Leaf(".Net项目组");
        Leaf a3 = new Leaf("软件测试组");
        Leaf a4 = new Leaf("产品研发组");
        Leaf b1 = new Leaf("国内市场部");
        Leaf b2 = new Leaf("国际市场部");
        Leaf c1 = new Leaf("客户服务部");
        Leaf c2 = new Leaf("商业客户部");
        Leaf c3 = new Leaf("业务发展部");
        Leaf c4 = new Leaf("人力行政部");
        Leaf c5 = new Leaf("人力资源部");

        // 组装一级部门
        root.addSubTissue(a);
        root.addSubTissue(b);
        root.addSubTissue(c);
        root.addSubTissue(d);
        root.addSubTissue(e);

        // 组装二级部门
        a.addSubTissue(a1);
        a.addSubTissue(a2);
        a.addSubTissue(a3);
        a.addSubTissue(a4);
        b.addSubTissue(b1);
        b.addSubTissue(b2);
        c.addSubTissue(c1);
        c.addSubTissue(c2);
        c.addSubTissue(c3);
        c.addSubTissue(c4);
        c.addSubTissue(c5);
        return root;
    }

    private static String getTreeInfo(Branch root) {
        List<AbstractTissue> subordinateList = root.getTissues();
        StringBuilder info = new StringBuilder();
        for (AbstractTissue tissue : subordinateList) {
            if (tissue instanceof Leaf) {
                info.append(tissue.getInfo()).append("\n");
            } else {
                // 是个小头目
                info.append(tissue.getInfo()).append("\n").append(getTreeInfo((Branch) tissue));
            }
        }
        return info.toString();
    }
}
